package com.extrace.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.extrace.server.config.AjaxResult;
import com.extrace.server.mapper.UserMapper;
import com.extrace.server.pojo.User;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@CrossOrigin
@RestController
public class UserController {

    @Resource
    UserMapper userMapper;

    /**
     *  管理员添加 查看快递员列表
     */
    @PostMapping("/Domain/User/getUserList")
    public AjaxResult getCustomerList(@RequestBody Map<String, Object> params){
        LambdaQueryWrapper<User> LambdaQueryWrapper=new LambdaQueryWrapper<>();
        if(params.containsKey("name") && StringUtils.hasLength(String.valueOf(params.get("name")))){
            LambdaQueryWrapper.like(User::getName, params.get("name"));
        }

        if(params.containsKey("telCode") && StringUtils.hasLength(String.valueOf(params.get("telCode")))){
            LambdaQueryWrapper.eq(User::getTelCode, params.get("telCode"));
        }

        if(params.containsKey("nid") && StringUtils.hasLength(String.valueOf(params.get("nid")))){
            LambdaQueryWrapper.like(User::getNid, params.get("nid"));
        }

        List<User> users = userMapper.selectList(LambdaQueryWrapper);
        return AjaxResult.success(users);
    }


    /**
     *  管理员更新 快递员信息
     */
    @PostMapping("/Domain/User/updateUser")
    public AjaxResult updateInfo(@RequestBody User user){
        return AjaxResult.success(userMapper.updateById(user));
    }


    /**
     *  管理员添加 快递员
     */
    @PostMapping("/Domain/User/addUser")
    public AjaxResult addInfo(@RequestBody User user){
        return AjaxResult.success(userMapper.insert(user));
    }

    /**
     *  管理员删除 快递员
     */
    @PostMapping("/Domain/User/deleteUser")
    public AjaxResult deleteInfo(@RequestBody Map<String, Object> params){
        Integer id = (Integer) params.get("id");
        return AjaxResult.success(userMapper.deleteById(id));
    }

}
